package com.spring.theory.bean.aware;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@Slf4j
public class AwareBean implements BeanNameAware, ApplicationContextAware, InitializingBean {

    // DI阶段
    @Autowired
    public void setApplicationContextWithAutowired(ApplicationContext applicationContext) {
        log.info("AwareBean自定义依赖注入ApplicationContext容器:" + applicationContext);
    }

    @Override
    public void setBeanName(String name) {
        log.info("AwareBean的BeanNameAware接口获取beanName: " + name);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("AwareBean的ApplicationContextAware接口获取容器: " + applicationContext);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("AwareBean使用InitializingBean的afterPropertiesSet");
    }

    @PostConstruct
    public void init() {
        log.info("AwareBean使用 @PostConstruct 注解初始化");
    }

}
